package com.mars.controller;

import com.mars.pojo.SysUser;
import com.mars.service.SysUserService;
import com.mars.util.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author MARS
 * @date 2018/1/9
 */

@Controller
public class UserController {

    @Resource
    private SysUserService sysUserService;

    /**
     * 用户登录
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/login.page")
    public void login(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        SysUser sysUser =  sysUserService.findBykeyword(username);

        String errorMsg = "";

        // 登录后跳转到原网页地址
        String ret = request.getParameter("ret");

        if (StringUtils.isBlank(username)) {
            errorMsg = "用户名不能为空";
        } else if (StringUtils.isBlank(password)){
            errorMsg = "密码不能为空";
        } else if (sysUser == null) {
            errorMsg = "查询不到指定用户";
        } else if (!sysUser.getPassword().equals(MD5Util.encrypt(password))) {
            errorMsg = "用户名或者密码错误";
        }  else if (sysUser.getStatus() != 1) {
            errorMsg = "用户已被冻结，联系管理员";
        } else {
            //login success

            // 将用户信息存到session中
            request.getSession().setAttribute("user",sysUser);

            // 当前ret不为空 ，跳转到ret的页面
            if (StringUtils.isNoneBlank(ret)) {
                response.sendRedirect(ret);
                return ;
            } else {
                //TODO
                String path = request.getContextPath();
                System.out.println(path);
                response.sendRedirect(path+"/admin/index.page");
                return ;
            }
        }

        // 将值返回
        request.setAttribute("error",errorMsg);
        request.setAttribute("username",username);
        if (StringUtils.isNotBlank(ret)) {
            request.setAttribute("ret",ret);

        }

        // 客户端跳转链接
        String path = "signin.jsp";
        request.getRequestDispatcher(path).forward(request,response);
        return ;
    }

    /**
     * 注销操作
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/logout.page")
    public void logout(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 移除session
        request.getSession().invalidate();
        // 客户端跳转
        String root = request.getContextPath();
        String path = "/signin.jsp";
        response.sendRedirect(root+path);

    }
}
